package nl.boelen.game.server.gui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import nl.boelen.game.server.entity.PlayerMP;
import nl.boelen.game.server.entity.Players;

@SuppressWarnings("serial")
public class TableModel extends AbstractTableModel {

    public String[] columnNames = { "PlayerName", "HP" ,"X", "Y", "Dir", "ip", "TCPport", "UDPport" };
    List<PlayerMP> connectedPlayers;
    private Players players;

    public TableModel(Players players, List<PlayerMP> connectedPlayers) {
        this.connectedPlayers = connectedPlayers;
        this.players = players;
    }

    @Override
    public synchronized int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public synchronized int getRowCount() {
        return connectedPlayers.size();
    }

    @Override
    public synchronized Object getValueAt(int rowIndex, int columnIndex) {
        PlayerMP p = players.getPlayerMP(rowIndex);

        if (p == null) {
            return null;
        }

        switch (columnIndex) {
            case 0:
                return p.getUserName();
            case 1:
                return p.getHp() + "/" + p.getMaxhp();
            case 2:
                return p.getX() >> 3; //Only count tiles, not pixels
            case 3:
                return p.getY() >> 3; //Only count tiles, not pixels
            case 4:
                return p.getMovingDirectionAsString();
            case 5:
                return p.getIp();
            case 6:
                return p.getTCPPort();
            case 7:
                return p.getUDPPort();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
}
